﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MC.RideEvent.Mvc.Models.Entity;
using MC.RideEvent.Mvc.Models.Repository;
using MC.RideEvent.Mvc.Models.ViewModel;

namespace MC.RideEvent.Mvc.Controllers
{
    public class MemberController : ControllerBase
    {
        private IWayRepository _wayRepo;
        private IMemberRepository _memberRepo;
        private IVehiculeRepository _vehiculeRepo;
        private IPreferenceRepository _preferenceRepo;

        public MemberController(IMemberRepository memberRep, IVehiculeRepository vehiculeRepo,
            IPreferenceRepository preferenceRepo, IWayRepository wayRepo)
        {
            _wayRepo = wayRepo;
            _memberRepo = memberRep;
            _vehiculeRepo = vehiculeRepo;
            _preferenceRepo = preferenceRepo;
        }

        public ActionResult Index()
        {
            var model = new ProfilMemberViewModel();
            model.Member = CurrentMember;
            var vehicules = _vehiculeRepo.GetVehiculesByMemberId(CurrentMember.Id);
            if (vehicules != null) model.Vehicules = vehicules;

            //The system check and load a vehicule's user if it exists.
            if(model.Vehicules != null)
            {
                model.Preference = _preferenceRepo.GetPreferenceByMemberId(CurrentMember.Id);

                //this ViewData["NbPlaceMaxVeicule"] have to be use in the "_PreferenceUserform" partial view
                //to indicate the number maximum of places available.
                //ViewData["NbPlaceMaxVeicule"] = model.Vehicule.NumberPlaces;
            }

            return View(model);
        }

        public ActionResult SubscriptionForm()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SubscriptionForm(MemberViewModel newMember)
        {
            if (ModelState.IsValid)
            {
                _memberRepo.SaveMember(newMember, false);
            }

            return View();
        }
        
        public ActionResult VehiculeCreateForm()
        {
            return PartialView("VehiculeCreateForm");
        }

        [HttpPost]
        public ActionResult VehiculeCreateForm(VehiculeViewModel vehicule)
        {
            vehicule.MemberId = CurrentMember.Id;

            if(ModelState.IsValid)
            {
                _vehiculeRepo.SaveVehicule(vehicule, false);
            }
            else
            {
                ModelState.AddModelError("VehiculeModelId", "test");
                return PartialView("VehiculeCreateForm", vehicule);
            }

            return RedirectToAction("Index", "Member");
        }

        public ActionResult VehiculeEditForm(int vehiculeId)
        {
            var model = _vehiculeRepo.GetVehiculeById(vehiculeId);

            return PartialView("VehiculeEditForm", model);
        }

        [HttpPost]
        public ActionResult VehiculeEditForm(VehiculeViewModel vehicule)
        {
            if (ModelState.IsValid)
            {
                vehicule.MemberId = CurrentMember.Id;
                _vehiculeRepo.SaveVehicule(vehicule, true);
            }

            return RedirectToAction("Index", "Member");
        }

        public ActionResult PreferenceForm()
        {
            var memberId = CurrentMember.Id;
            var model = _preferenceRepo.GetPreferenceByMemberId(memberId);
            return PartialView("PreferenceForm", model);
        }

        [HttpPost]
        public ActionResult PreferenceForm(PreferenceModel preference)
        {
            if(ModelState.IsValid)
            {
                var isUpdate = preference.Id != 0;
                var memberId = CurrentMember.Id;

                _preferenceRepo.SavePreference(preference, memberId, isUpdate);
            }

            return RedirectToAction("Index", "Member");
        }

        public ActionResult ListVehicules()
        {
            var memberId = int.Parse(CurrentMemberId);
            var list = _vehiculeRepo.GetVehiculesByMemberId(memberId);
            return View(list);
        }

        //public ActionResult ListDriverWays()
        //{
        //    List<WayViewModel> model = null;
        //    var member = CurrentMember;

        //    if (member.DriveLicenceDate != null)
        //    {
        //        model = _wayRepo.GetWaysByDriverId(member.Id);  
        //    }

        //    return View("ListDriverWays", model);
        //}

        public ActionResult ListPassagerWays()
        {
            var memberId = int.Parse(CurrentMemberId);
            var model = _wayRepo.GetWaysByPassagerId(memberId);

            return View(model);
        }

        #region PartialViews
        public ActionResult ListDriverWays()
        {
            List<WayViewModel> model = null;
            var member = CurrentMember;

            if (member.DriveLicenceDate != null)
            {
                model = _wayRepo.GetWaysByDriverId(member.Id);
            }

            return PartialView("ListDriverWays", model);
        }
        #endregion

        [HttpPost]
        public ActionResult LogIn(string emaillogin, string pwdlogin)
        {
            var currentMember = _memberRepo.MakeConnection(emaillogin, pwdlogin);

            if (currentMember != null)
            {
                CurrentMember = currentMember;
                CurrentMemberId = currentMember.Id.ToString();

                //Si un membre possede un ou plusieurs vehicule, le system les chargera.
                currentMember.Cars = _vehiculeRepo.GetVehiculesMember(CurrentMember.Id);
            }

            return RedirectToAction("Index", "Home");
        }

        public ActionResult LogOut()
        {
            ClearUserSession();

            return RedirectToAction("Index", "Home");
        }
    }
}
